package com.wwwarehouse.carddesk.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.activity.CardDeskActivity;
import com.wwwarehouse.carddesk.bean.CardDeskGroupGetGroupBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.carddesk.eventbus_event.CardDeskGroupEvent;
import com.wwwarehouse.carddesk.eventbus_event.CardDeskMessageEvent;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.adapter.carddesk.MyFragmentPagerAdapter;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.SlideBarView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

@Route(path = "/carddesk/DeskGroupFragment")
public class DeskGroupFragment extends BaseFragment {
    View v = null;
    private CustomViewPagerInternal mDeskGroupInvite;
    private CirclePageIndicator mDeskGroupIndicator;
    private SlideBarView mDeskGroupSlide;
    private ElasticScrollView mDeskGroupElastic;
    private StateLayout mDeskGroupLayout;
    private LinearLayout mLlResult;
    private ImageView mIvEmpty;
    private TextView mTvEmptyDesc;
    private TextView mTvEmpty;
    private LinearLayout mLlEmpty;
    private String searchText;
    private List<Fragment> mFragmentList = new LinkedList<>();
    private static final int REQUEST_GETGROUPMESSAGE = 10004;
    private int pageSize = 9;
    private int total;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_desk_group, container, false);
        initView(v);
        return v;
    }

    private void initView(View v) {
        mDeskGroupInvite = (CustomViewPagerInternal) v.findViewById(R.id.desk_group_invite);
        mDeskGroupIndicator = (CirclePageIndicator) v.findViewById(R.id.desk_group_indicator);
        mDeskGroupSlide = (SlideBarView) v.findViewById(R.id.desk_group_slide);
        mDeskGroupElastic = (ElasticScrollView) v.findViewById(R.id.desk_group_elastic);
        mDeskGroupLayout = (StateLayout) v.findViewById(R.id.desk_group_layout);
        mLlResult = (LinearLayout) v.findViewById(R.id.desk_group_result);
        mIvEmpty = (ImageView) v.findViewById(R.id.carddesk_group_empty_img);
        mTvEmpty = (TextView) v.findViewById(R.id.carddesk_group_empty_txt);
        mTvEmptyDesc = (TextView)v.findViewById(R.id.carddesk_group_empty_desc_txt);
        mLlEmpty = (LinearLayout) v.findViewById(R.id.carddesk_group_empty_lin);


        mDeskGroupElastic.setOnPullListener(new ElasticScrollView.OnPullListener() {
            @Override
            public void onDownPull() {
                ((BaseCardDeskActivity)mActivity).hideFilterBt();
                ((BaseCardDeskActivity)mActivity).hideSortBt();
                ((BaseCardDeskActivity)mActivity).downPull_4();
            }

            @Override
            public void onUpPull() {

            }
        });
        getAllGroupMes();
        EventBus.getDefault().register(this);
    }

    //搜索
    @Override
    public void searchNoRealTimeClick(String text) {
        if (peekFragment() == null && "group".equals(((CardDeskActivity) mActivity).getTabCurr())) {
            if (!TextUtils.isEmpty(text.trim())) {
                searchText = text;
                getAllGroupMes();
            }
        }
    }

    //取消
    @Override
    public void cancelClick() {
        if (peekFragment() == null && "group".equals(((CardDeskActivity) mActivity).getTabCurr())) {
            searchText = "";
            getAllGroupMes();
        }
    }

    /**
     * 显示卡片
     */
    public void showPageCard(){
        //总共多少数据 显示多少页
        mDeskGroupLayout.showContentView();

        if (total == 0) {
            mLlResult.setVisibility(View.GONE);
            mLlEmpty.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(searchText)) {
                //没有请求数据
                mTvEmpty.setText(R.string.teamdesk_group_nohave_txt);
                mTvEmptyDesc.setVisibility(View.VISIBLE);
            } else {
                //没有搜索结果
                mTvEmpty.setText(R.string.invite_search_no_result);
                mTvEmptyDesc.setVisibility(View.GONE);
            }
        } else {
            mLlResult.setVisibility(View.VISIBLE);
            mLlEmpty.setVisibility(View.GONE);

            int page = (total % 9 == 0) ? total / 9 : (total / 9 + 1);
            mFragmentList.clear();
            GroupDetailsFragment messageDetailsFragment;
            for (int i = 0; i < page; i++) {
                messageDetailsFragment = new GroupDetailsFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("page",i+1);
                bundle.putString("personName",searchText);
                messageDetailsFragment.setArguments(bundle);
                mFragmentList.add(messageDetailsFragment);
            }

            mDeskGroupInvite.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), mFragmentList));

            /** 底部slidebar显示问题  */
            if (page <= 6) {
                if (page == 1) {
                    mDeskGroupIndicator.setVisibility(View.GONE);
                    mDeskGroupSlide.setVisibility(View.GONE);
                } else {
                    mDeskGroupIndicator.setVisibility(View.VISIBLE);
                    mDeskGroupSlide.setVisibility(View.GONE);

                    mDeskGroupIndicator.setViewPager(mDeskGroupInvite, 0);
                }
            } else {
                mDeskGroupIndicator.setVisibility(View.GONE);
                mDeskGroupSlide.setVisibility(View.VISIBLE);

                mDeskGroupSlide.setTotalPage(page);
                mDeskGroupSlide.setOnSlideChange(new SlideBarView.OnSlideChange() {
                    @Override
                    public void onSlideChange(int page) {
                        mDeskGroupInvite.setCurrentItem(page, false);
                    }
                });

                mDeskGroupSlide.setViewPagerInternal(mDeskGroupInvite);
            }
        }
    }


    public void getAllGroupMes(){
        Map<String , Object > map = new HashMap<>();
        /**
         * page		number
         personName	姓名	string	非必填 搜索
         size		number
         */
        map.put("page",1);
        map.put("size" , pageSize);
        map.put("personName" , searchText);

        NoHttpUtils.httpPost(CardDeskConstant.PATH_CARDDESK_GROUP_GETGROUP_MESSAGE,map,myOnresponse,REQUEST_GETGROUPMESSAGE);
        mDeskGroupLayout.showProgressView(false);

    }

    NoHttpUtils.OnResponseListener myOnresponse = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what){
                case REQUEST_GETGROUPMESSAGE:
                    if("0".equals(commonClass.getCode())){
                        CardDeskGroupGetGroupBean bean = JSON.parseObject(commonClass.getData().toString(),CardDeskGroupGetGroupBean.class);
                        total = bean.getTotal();
                        showPageCard();
                    }else{

                        mDeskGroupLayout.showSystemView("" + commonClass.getMsg() , false);
                        mDeskGroupLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mDeskGroupLayout.showProgressView(false);
                                getAllGroupMes();
                            }
                        });
                    }

                    break;

                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            mDeskGroupLayout.showSystemView(false);
            mDeskGroupLayout.setSystemListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDeskGroupLayout.showProgressView(false);
                    getAllGroupMes();
                }
            });
        }

        @Override
        public void onFinish(int what) {
            mDeskGroupLayout.showContentView();
        }
    };

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }

    @Override
    public void onPause() {
        super.onPause();
        ((BaseCardDeskActivity)mActivity).showFilterBt();
        ((BaseCardDeskActivity)mActivity).showSortBt();
    }

    //页面切换的事件
    public void onEventMainThread(CardDeskMessageEvent event) {
        if("group".equals(event.getMsg())){
            getAllGroupMes();
        }else{
            ((BaseCardDeskActivity)mActivity).showFilterBt();
            ((BaseCardDeskActivity)mActivity).showSortBt();
        }
    }
    //页面切换的事件
    public void onEventMainThread(CardDeskGroupEvent event) {
        if("refresh".equals(event.getMsg())){
            getAllGroupMes();
            ((BaseCardDeskActivity)mActivity).cleanBackStack();
        }else{
            ((BaseCardDeskActivity)mActivity).cleanBackStack();
        }
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}
